<?php
namespace Qgs\Controller;

use Think\Controller;
use Think\Model;


class StoresetupController extends BaseController {

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
        if (!$this->local_id) $this->local_id = 1;
    }

    /**
     * 店铺配置
     */
    public function index() {
        $m = M('webpos_local_store a');

        $store = M('webpos_local_store')->where(['local_id' => $this->local_id])->find();
        if ($store['services']) {

            $store['services'] = json_decode($store['services'], true);
        }
        if ($_POST) {
            //var_dump($_POST);return;
            //area_dispatch tel mobile ziti_remark wenxintishi
            $area_dispatch = I('area_dispatch', '', htmlspecialchars());
            $tel = I('tel', '', htmlspecialchars());
            $mobile = I('mobile', '', htmlspecialchars());
            $ziti_remark = I('ziti_remark', '', htmlspecialchars());
            $wenxintishi = I('wenxintishi', '');
            $services = I('services', []);
            $maxPriceDesc = I('maxPriceDesc', '');
            $services = array_filter($services);
            sort($services);
            $m = M('webpos_local_store');

            $data = [
                'area_dispatch' => $area_dispatch,
                'tel' => $tel,
                'mobile' => $mobile,
                'ziti_remark' => $ziti_remark,
                'wenxintishi' => $wenxintishi,
                'maxPriceDesc' => $maxPriceDesc,
                'services' => empty(!$services) ? json_encode($services) : '',
            ];
            try {
                $local_save = $m->where(['local_id' => $this->local_id])->save($data);
                if ($local_save === false) {
                    throw new \Exception(false);
                }
                M()->execute('commit');
                $this->success('修改成功', U('index'));
            }catch (\Exception $e){
                M()->execute('rollback');
                $this->error('修改失败');
            }
            exit;
        }

        $this->assign(['store' => $store]);
        $this->display();
    }

}